package com.cgtong.android.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.AsyncTask;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Sky.Bing on 2017/10/30.
 */

/**
 * 给图片添加水印功能
 */
public class WaterMarkerBitmap {
    private Context mContext;
    private String finalFilePath;
    private int markerResourceId;
    private AddWaterMarkerCallBack markerCallBack;

    public WaterMarkerBitmap(Context context) {
        mContext = context;
    }

    public void doAddWaterMarkerTask(String filePah, int markerResId, AddWaterMarkerCallBack addWaterMarkerCallBack) {
        finalFilePath = filePah;
        markerResourceId = markerResId;
        markerCallBack = addWaterMarkerCallBack;
        if (StringUtils.isEmpty(finalFilePath)) {
            LogUtils.i("Unable to decode stream: filePath is empty");
            return;
        }
        new AddWaterMarkerTask().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
    }

    public class AddWaterMarkerTask extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {
            return doAddWaterMarker();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (null != markerCallBack) {
                markerCallBack.onAddWaterMarkerSuccess(s);
            }
        }
    }

    public String doAddWaterMarker() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap originBmp, finalBmp, markerBmp;
        originBmp = BitmapFactory.decodeFile(finalFilePath, options);
        int originBmpWidth = options.outWidth;
        int originBmpHeight = options.outHeight;
        options.inJustDecodeBounds = false;
        finalBmp = BitmapFactory.decodeFile(finalFilePath, options).copy(Bitmap.Config.ARGB_8888, true);
        markerBmp = BitmapFactory.decodeResource(mContext.getResources(), markerResourceId, options);
        int markerBmpWidth = markerBmp.getWidth();
        int markerBmpHeight = markerBmp.getHeight();
        Canvas canvas = new Canvas(finalBmp);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Rect srcRect = new Rect(0, 0, markerBmpWidth, markerBmpHeight);
        /*if (markerBmpWidth > originBmpWidth) {
            markerBmpWidth = originBmpWidth;
        }*/
        int left = originBmpWidth / 4; //- markerBmpWidth / 2 + 20;
        int top = originBmpHeight / 2 - markerBmpHeight / 2;
        Rect destRect = new Rect(left, top, left + originBmpWidth / 2, top + markerBmpHeight);
        //canvas.drawBitmap(markerBmp, left, top, paint);
        canvas.drawBitmap(markerBmp, srcRect, destRect, paint);
        paint.setColor(Color.WHITE);
        paint.setTextSize(45);
        String nowTime = TimeUtils.getNowString();
        String drawText = "";
        try {
            StringBuffer stringBuffer = new StringBuffer();
            String[] yearAndMonthAndDay = nowTime.substring(0, 10).split("-");
            for (int i = 0; i < yearAndMonthAndDay.length; i++) {
                String str = yearAndMonthAndDay[i];
                if (i == yearAndMonthAndDay.length - 1) {
                    stringBuffer.append(str);
                } else {
                    stringBuffer.append(str + ".");
                }
            }
            stringBuffer.append(" - ");
            stringBuffer.append(nowTime.substring(11, nowTime.length()));
            drawText = stringBuffer.toString();
        } catch (Exception e) {
            drawText = nowTime;
        }
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        int textHeight = fontMetrics.bottom - fontMetrics.top;
        Rect bounds = new Rect();
        paint.getTextBounds(drawText, 0, drawText.length(), bounds);
        int textWidth = bounds.right - bounds.left;

        int baseX = originBmpWidth - textWidth - 10;
        int textTop = originBmpHeight - textHeight;
        int baseY = textTop - fontMetrics.top;
        canvas.drawText(drawText, baseX, baseY, paint);
        try {
            FileOutputStream outputStream = new FileOutputStream(finalFilePath);
            if (finalBmp.compress(Bitmap.CompressFormat.PNG, 100, outputStream)) {
                outputStream.flush();
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return finalFilePath;
    }

    public interface AddWaterMarkerCallBack {
        void onAddWaterMarkerSuccess(String path);
    }
}